// pages/getDetail/getDetail.js
let videoAd = null
let db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getDetail:{},
    tab:1,
    isUserLogin:false,
    zhuli:'2',
    chooseIndex:0,
    maskShow:false,
    videoList:[]
  },
  hideMask(){
    this.setData({
      maskShow:false
    })
  },
  setZhuli(e){
    let {index}=e.currentTarget.dataset
    this.setData({
      zhuli:index
    })
  },
  isUserLogin(){
    let isUserLogin
    if(wx.getStorageSync('userInfo')){
      isUserLogin=true
    } else {
      isUserLogin=false
    }
    this.setData({isUserLogin,userInfo:wx.getStorageSync('userInfo')})
    this.getCount()
    wx.hideLoading({
      success: (res) => {},
    })
  },
  watchAll(){
    wx.setStorageSync('userList', this.data.getDetail.user)
    wx.navigateTo({
      url: '/pages/userList/userList',
    })
  },
  tabChoose(e){
    this.setData({
      tab:e.currentTarget.dataset.index
    })
  },
  gengxin(){
    wx.setClipboardData({
      data: this.data.videoList[this.data.chooseIndex].http,
      success (res) {
        
      }
    })
    this.setData({
      maskShow:true
    })
  },
  choujiang(e){
    let {type,index} = e.currentTarget.dataset
    this.setData({
      chooseIndex:index
    })
    if(type!=1){
      
      this.gengxin()
      return
    }
    wx.showLoading({
      title: '请稍等...',
      mask:true
    })
    if (videoAd) {
      videoAd.show()
        .then(() => {
          wx.hideLoading({
            success: (res) => {},
          })
        })
        .catch(() => {
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showToast({
            title: '请5s后重试',
            duration:2000
          })
        })
    } else {
      wx.hideLoading({
        success: (res) => {},
      })
    }
  },
  getCount(){
    let getDetail = this.data.getDetail
    let count = 0
    let openId = wx.getStorageSync('openId')
    getDetail.user.forEach(element => {
      if(element.openId == openId){
        count+=1
      }
    });
    getDetail.joinCount = count
    this.setData({getDetail})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().getRandomAd(true,this)
    this.getRandomAd()
    this.getRandomAd(true)
    let that = this
    wx.showLoading({
      title: '请稍等...',
    })
    db.collection('videoList').get().then(res => {
      console.log(res)
      this.setData({
        videoList:res.data[0].list
      })
      wx.hideLoading({
        success: (res) => {},
      })
  })
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: getApp().globalData.jili
      })
      videoAd.onLoad(() => { })
      videoAd.onError((err) => { })
      videoAd.onClose((status) => { 
        if (status && status.isEnded || status === undefined) {
          // 正常播放结束，下发奖励
          // continue you code
          that.gengxin()
          console.log('已完成')
        } else {
          console.log('未完成')
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRandomAd()
    getApp().chaping()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    
  }
})